<?php

declare(strict_types=1);

namespace App\Controller\Home\Drp;


use App\Controller\AbstractController;
use App\Model\Bill;
use App\Model\CardOrder;
use App\Model\Distributors;
use App\Model\NfcCard;
use App\Model\Normal;
use App\Services\AdapayService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Trait\ResponseMsg;
use Util\Common;

class CardController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Distributors $distributors;

    #[inject]
    protected AdapayService $adapayService;

    #[inject]
    protected NfcCard $nfcCard;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }


    public function createOrder(RequestInterface $request){
        $info = $this->distributors->getInfo($request);
        if (!$info) {
            return $this->error(code: '400', msg: '登录态失效 请重新登录');
        }
        $params = $request->inputs(['money', 'card_no', 'pay_type']);

        $check = $this->nfcCard->where('card_no',$params['card_no'])->first();
        if(!$check){
            return $this->error(404,'卡号不存在');
        }
        if(is_string($params['card_no'])){
            $params['card_no'] = trim($params['card_no']);
        }
        $save['card_no'] = $params['card_no'];
        $save['number'] = $this->normal->randOrder($info['id']);
        $save['fee'] = $params['money'];
        $save['payment_type'] = $params['pay_type'];
        try {
            $orderModel = CardOrder::create($save);
            if (!$orderModel) {
                return $this->error(code: '1002', msg: '订单创建失败');
            }
            if ($params['pay_type'] == Bill::PAYMENT_TYPE_WE_CHAT_PAY) {
                $expend = ['open_id' => $info['openid']];
                $notifyUrl = Common::MAIN_HOST . '/user/order/card_notify';
            } else {
                $expend = ['buyer_id' => $info['openid']];
                $notifyUrl = Common::MAIN_HOST . '/user/order/card_notify';
            }
            $payment_params = array(
                "app_id" => $this->adapayService->appId,
                "order_no" => $orderModel->number,
                "pay_channel" => $params['pay_type'],
                "time_expire" => date("YmdHis", time() + 300),
                "pay_amt" => sprintf("%.2f", $orderModel->fee),
                "goods_title" => $info['phone'] . '充值卡号'.$params['card_no'].'-' . $orderModel->fee,
                "goods_desc" => $info['phone'] . '充值卡号'.$params['card_no'].'-' . $orderModel->fee,
                "description" => $info['phone'] . '充值卡号'.$params['card_no'].'-' . $orderModel->fee,
                "expend" => $expend,
                "notify_url" => $notifyUrl
            );
            $result = $this->adapayService->Payment($payment_params);
            return $this->success($result);
        } catch (\Exception $e) {
            return $this->error(1001, '操作繁忙 请稍后再试');
        }
    }


}
